import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.*;

class Student {
    private String studentId;
    private String name;

    public Student(String studentId, String name) {
        this.studentId = studentId;
        this.name = name;
    }

    public String getStudentId() {
        return studentId;
    }

    public String getName() {
        return name;
    }

    @Override
    public String toString() {
        return "学号: " + studentId + ", 姓名: " + name;
    }
}

public class RandomStudentID {
    public static void main(String[] args) {
        String excelFilePath = "D:\\java\\gzeu\\gzeu\\软件班-花名册.xlsx";
        List<Student> students = new ArrayList<>();

        try (FileInputStream fis = new FileInputStream(new File(excelFilePath));
             Workbook workbook = new XSSFWorkbook(fis)) {

            Sheet sheet = workbook.getSheetAt(0);

            for (Row row : sheet) {
                if (row.getRowNum() == 0) continue;

                Cell idCell = row.getCell(0);
                Cell nameCell = row.getCell(1);

                String studentId = idCell != null ? idCell.toString() : "";
                String name = nameCell != null ? nameCell.toString() : "";

                if (!studentId.isEmpty() && !name.isEmpty()) {
                    students.add(new Student(studentId, name));
                }
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
        //77

        int numberOfStudentsToSelect = 13;
        List<Student> selectedStudents = getRandomStudents(students, numberOfStudentsToSelect);

        System.out.println("随机选取的 " + numberOfStudentsToSelect + " 个学生:");
        for (Student student : selectedStudents) {
            System.out.println(student);
        }
    }
    public static List<Student> getRandomStudents(List<Student> allStudents, int count) {
        if (count <= 0 || allStudents.isEmpty()) return Collections.emptyList();
        if (count > allStudents.size()) count = allStudents.size();

        List<Student> shuffled = new ArrayList<>(allStudents);
        Collections.shuffle(shuffled);
        return shuffled.subList(0, count);
    }
}

